<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<PSAdmin.Core.Security.UserRole>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	Edit <%=Html.Encode(Model.Name) %> Role
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <div class="span-12">
        <h2>Edit <%=Model.Name %> Role</h2>
    </div>
    <div class="span-12 last right">
        <%using (Html.BeginForm("DeleteRole", "Security", new { id = Model.Id })) { %>
            <input type="submit" value="Delete" id="deleteRole" />
        <%} %>
    </div>
    <div class="clear"></div>   
    <%=Html.ValidationSummary() %>
    <% using (Html.BeginForm()) {%>
        <fieldset>
            <legend>
                Fields
            </legend>
            <p>
                <%=Html.LabelFor(m => m.Name) %>    
                <%=Html.TextBoxFor(m => m.Name) %>
            </p>
            <p>
                <input type="submit" value="Save" />
            </p> 
            <% if (Model.HasUsers) { %>
            <div class="roleusers">
                <h3>Users in this role</h3>
                <ul>
                    <%foreach(var user in Model.Users) { %>
                        <li>
                            <%=Html.Encode(user.Username) %> - 
                            <a href="<%=Url.Action("RemoveUserFromRole", new { id = Model.Id, userId = user.Id }) %>" onclick="RemoveUser(this);return false;">Remove</a></td>
                        </li>
                    <%} %>
                </ul>
            </div>    
            <%} %>          
        </fieldset>
    <%} %>
    
    <%using (Html.BeginForm("AddUserToRole", "Security", new { id = Model.Id }))
      { %>
    
        <fieldset>
            <legend>
                Add User To Role
            </legend>
            <p>
                <label for="username">Username (e.g. domain\username)</label>
                <%=Html.TextBox("username")%>
            </p>
            <p>
                <input type="submit" value="Add" />
            </p>
        </fieldset>
    
    <%} %>
    
    <div>
        <%=Html.ActionLink("Back to List", "Index") %>
    </div>
    <script type="text/javascript">

        $(document).ready(function() {
            $("#deleteRole").click(function() {
                var response = confirm("Are you sure you want to delete this role?");
                if (response == false)
                    return false;
            });
        });

        function RemoveUser(link) {

            var url = $(link).attr('href');

            $.post(url, null, function(data) {
                if (data == true) {
                    $(link).parents('li').remove();
                }
            }, "json");
        }
    </script>
</asp:Content>
